from DingDingBot.DDBOT import DingDing
from flask import current_app


class InterfaceCasePush(object):
    
    @classmethod
    def __init__(cls, result):
        # 项目图片-目前不好用
        cls.url = result.get('url')
        # 链接地址
        project_id = result.get('project_id')
        report_id = result.get('report_id')
        cls.msg_url = 'http://jlb-tc.bestjlb.cn/#/project/{}/manage/reportShow?reportId={}'.format(
            project_id, report_id)
        # 标题
        project_name = result.get('project_name')
        cls.title = '服务名称: ' + project_name
        task_name = result.get('task_name')
        cls.task_name = '任务名称: ' + task_name
        
        cls.all_scene = result.get('all_scene')
        cls.successes = result.get('successes')
        if cls.successes is None:
            cls.successes = 0
        cls.failures = result.get('failures')
        if cls.failures is None:
            cls.failures = 0
        cls.link_name = '<font size="3px">报告地址</font>'
        token = result.get('dd_token')
        if len(token) == 64:
            cls.token = 'https://oapi.dingtalk.com/robot/send?access_token=' + token
        else:
            cls.token = token
    
    @classmethod
    def interface_push_markdown(cls, result):
        try:
            result = InterfaceCasePush(result)
            # 初始话DingDingBOt
            dd = DingDing(result.token)
            # 拼接content
            content = cls.get_markdown_content(result)
            print(dd.Send_MardDown_Msg(Title=result.title, Content=content, atMobiles=[],
                                       isAtAll=False))
        except Exception as error:
            current_app.logger.info(str(error))
    
    @classmethod
    def get_markdown_content(cls, result):
        """
        拼接content
        :param result:
        :return: content
        """
        total = '{title}\n\n' \
                '{task_name}\n\n' \
                '[{link_name}]({msg_url})\n\n' \
                '<font color="#00142a" size="3px">\n\n' \
                '总数用例: {all_scene}</font>\n\n'.format(title=result.title,
                                                      task_name=result.task_name,
                                                      link_name=result.link_name,
                                                      msg_url=result.msg_url,
                                                      all_scene=result.all_scene)
        
        case_text = '<font color="#00142a" size="2px">' \
                    '成功用例{}</font>\n\n' \
                    '<font color="#f56c6c" size="2px">' \
                    '失败用例{}</font>'.format(result.successes, result.failures)
        content = total + case_text
        return content
    
    @classmethod
    def get_push_text_content(cls, result):
        content = '{}\n\n' \
                  '总数用例{}\n\n' \
                  '成功用例{}\n\n' \
                  '失败用例{}\n\n' \
                  '错误用例{}\n\n'.format(result.link_name,
                                      result.all_scene,
                                      result.successes,
                                      result.failures,
                                      result.error)
        return content
